﻿using SetPriceCalculateV1d0.Entity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SetPriceCalculateV1d0.Service
{
    public class HotchPotchV1d0
    {
        /// <summary>
        /// 笛卡尔乘积
        /// </summary>
        public static List<List<T>> Descartes<T>(List<List<T>> lstSplit)
        {
            int count = 1;
            //lstSplit.ForEach(item => count *= item.Count);
            foreach (var item in lstSplit)
            {
                if (item.Count > 0) count *= item.Count;
            }

            var lstResult = new List<List<T>>();

            for (int i = 0; i < count; ++i)
            {
                var lstTemp = new List<T>();
                int j = 1;
                lstSplit.ForEach(item =>
                {
                    j *= item.Count;
                    lstTemp.Add(item[(i / (count / j)) % item.Count]);
                });
                lstResult.Add(lstTemp);
            }
            return lstResult;
        }
        /// <summary>
        /// 得到每个【配菜】菜品的对应的规则集
        /// </summary>
        /// <returns></returns>
        public static List<List<ItemSetPriceRuleV1d0>> GetSingleItemValidRuleListList(ItemSetPriceRuleSetV1d0 ruleSet, ItemPriceInfoSetV1d0 priceInfoSet)
        {
            List<List<ItemSetPriceRuleV1d0>> ruleListList = new List<List<ItemSetPriceRuleV1d0>>();
            var subItemPriceInfoSet = GetSubItemPriceInfoSet(priceInfoSet);
            foreach (var item in subItemPriceInfoSet.ItemPriceInfoList)
            {
                List<ItemSetPriceRuleV1d0> ruleList = new List<ItemSetPriceRuleV1d0>();
                //规则集中配菜级别等于当前配菜菜品级别的规则，对当前配菜菜品才是有效的
                var filterSubLevelRuleList = ruleSet.ItemSetPriceRuleList.FindAll(x => x.SubLevel == item.ItemLevel);
                //再次进行筛选,规则列表中,主菜级别包含在已点菜品中,该规则才有效
                foreach (var rule in filterSubLevelRuleList)
                {
                    var finded = priceInfoSet.ItemPriceInfoList.FirstOrDefault(x => x.ItemLevel == rule.MainLevel);
                    if (finded != null) ruleList.Add(rule);
                }
                //增加一个特殊规则，有配菜，无主菜的情况,套餐价格为配菜原本价格
                ruleList.Add(new ItemSetPriceRuleV1d0("Special", 999, item.ItemLevel, item.ItemPrice));
                ruleListList.Add(ruleList);
            }
            return ruleListList;
        }
        /// <summary>
        /// 得到已点菜品中的配菜列表
        /// </summary>
        /// <param name="totalPriceInfoSet"></param>
        /// <returns></returns>
        public static ItemPriceInfoSetV1d0 GetSubItemPriceInfoSet(ItemPriceInfoSetV1d0 totalPriceInfoSet)
        {
            ItemPriceInfoSetV1d0 subSet = new ItemPriceInfoSetV1d0();
            subSet.ItemPriceInfoList = totalPriceInfoSet.ItemPriceInfoList.FindAll(x => x.ItemLevel >= 50);
            return subSet;
        }


    }
}
